package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.task.ArticleHtmlTask;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author syn
 * @since 2022-01-01
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
   private ArticleHtmlTask articleHtmlTask;
    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {

        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(dto, article);
        //保存两张表的数据ap_article和ap_article_content
        //进行判断,如果dto中携带id就不是保存了(等于null就是保存)
        if (dto.getId() != null) {//意味着数据的修改
            //修改ap_article
            this.updateById(article);
            //修改ap_article_content
            //update ap_article_content set content=?? where article_id=??
            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ApArticleContent::getArticleId, dto.getId());//where article_id=??
            updateWrapper.set(ApArticleContent::getContent, dto.getContent());//set content=??
            apArticleContentService.update(updateWrapper);
            //异步生成文章的静态页面
            articleHtmlTask.createHtml(article,dto.getContent());
            return ResponseResult.okResult(article.getId());

        } else {//意味着数据的保存
            //保存ap_article
            article.setCreatedTime(new Date());
            this.save(article);
            //保存ap_article_content
            ApArticleContent apArticleContent = new ApArticleContent();
            //走雪花算法,不需要赋值
            //apArticleContent.setId();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
            //异步生成文章的静态页面
            articleHtmlTask.createHtml(article,dto.getContent());
            return ResponseResult.okResult(article.getId());
        }
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        //查询发布时间大于dto中的maxTime
        if (type == 0) {
            query.gt(ApArticle::getPublishTime, dto.getMaxTime());
        } else {
            query.lt(ApArticle::getPublishTime, dto.getMinTime());
        }
        //如果dto中的channelId不为0时,根据频道查询
        if (dto.getChannelId() != 0) {
            query.eq(ApArticle::getChannelId, dto.getChannelId());
        }
//        隐藏条件1是否删除2是否下架
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        //根据发布时间降序排序
        query.orderByDesc(ApArticle::getCreatedTime);
//        分页查询
        IPage<ApArticle> page = new Page<>(1, dto.getSize());
        IPage<ApArticle> page1 = this.page(page, query);
        return ResponseResult.okResult(page1.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle == null || apArticle.getIsDelete() || apArticle.getIsDown()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 根据id查询文章内容
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(query);
        // 按照前端的格式拼接map
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        // 返回通用的响应
        return ResponseResult.okResult(map);
    }
}
